package cn.tedu.zhyl.base.config;

import cn.tedu.zhyl.base.interceptors.LoginAuthInterceptor;
import cn.tedu.zhyl.base.interceptors.WxLoginAuthInterceptor;
import cn.tedu.zhyl.base.properties.LoginAuthProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private LoginAuthInterceptor loginAuthInterceptor;
    @Autowired
    private LoginAuthProperties loginAuthProperties;
    @Autowired
    private WxLoginAuthInterceptor wxLoginAuthInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry
                .addMapping("/**") // 前端请求路径
                .allowedHeaders("*") // 允许携带任意的请求头
                .allowedOriginPatterns("*") // 允许任意的源(域)发来QQ
                .allowCredentials(true) // 允许携带Cooke
                .maxAge(3600); // 会话信息保留时间,1h不再重复检测,直接放行
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 小程序、暂时用一套拦截
        registry.addInterceptor(loginAuthInterceptor)
                .excludePathPatterns(loginAuthProperties.getNoAuthUrls())
                .addPathPatterns("/**");


        // 小程序拦截器
//        registry.addInterceptor(wxLoginAuthInterceptor)
//                .excludePathPatterns(loginAuthProperties.getNoAuthUrls())
//                .addPathPatterns("/customer/**");

    }


}
